package com.cencat.driver.entity;

import com.baomidou.mybatisplus.annotation.*;
import com.cencat.common.entity.BaseEntity;
import jakarta.validation.constraints.*;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;

/**
 * 司机实体类
 * 
 * @author cencat
 * @since 2024-01-01
 */
@Data
@EqualsAndHashCode(callSuper = true)
@TableName("driver")
public class Driver extends BaseEntity {

    /**
     * 司机ID
     */
    @TableId(value = "id", type = IdType.AUTO)
    private Long id;

    /**
     * 司机编码
     */
    @NotBlank(message = "司机编码不能为空")
    @Size(max = 32, message = "司机编码长度不能超过32个字符")
    @TableField("driver_code")
    private String driverCode;

    /**
     * 司机姓名
     */
    @NotBlank(message = "司机姓名不能为空")
    @Size(max = 50, message = "司机姓名长度不能超过50个字符")
    @TableField("driver_name")
    private String driverName;

    /**
     * 手机号
     */
    @NotBlank(message = "手机号不能为空")
    @Pattern(regexp = "^1[3-9]\\d{9}$", message = "手机号格式不正确")
    @TableField("phone")
    private String phone;

    /**
     * 身份证号
     */
    @NotBlank(message = "身份证号不能为空")
    @Pattern(regexp = "^[1-9]\\d{5}(18|19|20)\\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\\d{3}[0-9Xx]$", message = "身份证号格式不正确")
    @TableField("id_card")
    private String idCard;

    /**
     * 驾驶证号
     */
    @NotBlank(message = "驾驶证号不能为空")
    @Size(max = 20, message = "驾驶证号长度不能超过20个字符")
    @TableField("driver_license")
    private String driverLicense;

    /**
     * 驾照类型：A1,A2,A3,B1,B2,C1,C2
     */
    @NotBlank(message = "驾照类型不能为空")
    @Pattern(regexp = "^(A1|A2|A3|B1|B2|C1|C2)$", message = "驾照类型不正确")
    @TableField("license_type")
    private String licenseType;

    /**
     * 驾照到期日期
     */
    @NotNull(message = "驾照到期日期不能为空")
    @Future(message = "驾照到期日期必须是未来日期")
    @TableField("license_expire_date")
    private LocalDate licenseExpireDate;

    /**
     * 绑定车辆ID
     */
    @TableField("vehicle_id")
    private Long vehicleId;

    /**
     * 状态：0-离线，1-在线，2-忙碌，3-休息，4-禁用
     */
    @Min(value = 0, message = "状态值不能小于0")
    @Max(value = 4, message = "状态值不能大于4")
    @TableField("status")
    private Integer status;

    /**
     * 工作状态：0-空闲，1-配送中，2-等待接单，3-暂停接单
     */
    @Min(value = 0, message = "工作状态值不能小于0")
    @Max(value = 3, message = "工作状态值不能大于3")
    @TableField("work_status")
    private Integer workStatus;

    /**
     * 当前位置经度
     */
    @DecimalMin(value = "-180.0", message = "经度值不能小于-180")
    @DecimalMax(value = "180.0", message = "经度值不能大于180")
    @TableField("current_location_lng")
    private BigDecimal currentLocationLng;

    /**
     * 当前位置纬度
     */
    @DecimalMin(value = "-90.0", message = "纬度值不能小于-90")
    @DecimalMax(value = "90.0", message = "纬度值不能大于90")
    @TableField("current_location_lat")
    private BigDecimal currentLocationLat;

    /**
     * 当前地址
     */
    @Size(max = 255, message = "当前地址长度不能超过255个字符")
    @TableField("current_address")
    private String currentAddress;

    /**
     * 服务区域（JSON格式存储多边形坐标）
     */
    @TableField("service_area")
    private String serviceArea;

    /**
     * 评分
     */
    @DecimalMin(value = "0.0", message = "评分不能小于0")
    @DecimalMax(value = "5.0", message = "评分不能大于5")
    @TableField("rating")
    private BigDecimal rating;

    /**
     * 评价数量
     */
    @Min(value = 0, message = "评价数量不能小于0")
    @TableField("review_count")
    private Integer reviewCount;

    /**
     * 总订单数
     */
    @Min(value = 0, message = "总订单数不能小于0")
    @TableField("total_orders")
    private Integer totalOrders;

    /**
     * 完成订单数
     */
    @Min(value = 0, message = "完成订单数不能小于0")
    @TableField("completed_orders")
    private Integer completedOrders;

    /**
     * 取消订单数
     */
    @Min(value = 0, message = "取消订单数不能小于0")
    @TableField("cancelled_orders")
    private Integer cancelledOrders;

    /**
     * 总行驶里程（公里）
     */
    @DecimalMin(value = "0.0", message = "总行驶里程不能小于0")
    @TableField("total_distance")
    private BigDecimal totalDistance;

    /**
     * 总收入
     */
    @DecimalMin(value = "0.0", message = "总收入不能小于0")
    @TableField("total_income")
    private BigDecimal totalIncome;

    /**
     * 头像URL
     */
    @Size(max = 255, message = "头像URL长度不能超过255个字符")
    @TableField("avatar")
    private String avatar;

    /**
     * 紧急联系人
     */
    @Size(max = 50, message = "紧急联系人长度不能超过50个字符")
    @TableField("emergency_contact")
    private String emergencyContact;

    /**
     * 紧急联系电话
     */
    @Pattern(regexp = "^1[3-9]\\d{9}$", message = "紧急联系电话格式不正确")
    @TableField("emergency_phone")
    private String emergencyPhone;

    /**
     * 入职日期
     */
    @NotNull(message = "入职日期不能为空")
    @TableField("join_date")
    private LocalDate joinDate;

    /**
     * 最后在线时间
     */
    @TableField("last_online_time")
    private LocalDateTime lastOnlineTime;

    /**
     * 最后位置更新时间
     */
    @TableField("last_location_update_time")
    private LocalDateTime lastLocationUpdateTime;

    /**
     * 租户ID
     */
    @NotNull(message = "租户ID不能为空")
    @TableField("tenant_id")
    private Long tenantId;

    // 状态枚举常量
    public static final int STATUS_OFFLINE = 0;  // 离线
    public static final int STATUS_ONLINE = 1;   // 在线
    public static final int STATUS_BUSY = 2;     // 忙碌
    public static final int STATUS_REST = 3;     // 休息
    public static final int STATUS_DISABLED = 4; // 禁用

    // 工作状态枚举常量
    public static final int WORK_STATUS_IDLE = 0;        // 空闲
    public static final int WORK_STATUS_DELIVERING = 1;  // 配送中
    public static final int WORK_STATUS_WAITING = 2;     // 等待接单
    public static final int WORK_STATUS_PAUSED = 3;      // 暂停接单

    /**
     * 获取状态描述
     */
    public String getStatusDesc() {
        switch (this.status) {
            case STATUS_OFFLINE: return "离线";
            case STATUS_ONLINE: return "在线";
            case STATUS_BUSY: return "忙碌";
            case STATUS_REST: return "休息";
            case STATUS_DISABLED: return "禁用";
            default: return "未知";
        }
    }

    /**
     * 获取工作状态描述
     */
    public String getWorkStatusDesc() {
        switch (this.workStatus) {
            case WORK_STATUS_IDLE: return "空闲";
            case WORK_STATUS_DELIVERING: return "配送中";
            case WORK_STATUS_WAITING: return "等待接单";
            case WORK_STATUS_PAUSED: return "暂停接单";
            default: return "未知";
        }
    }

    /**
     * 计算完成率
     */
    public BigDecimal getCompletionRate() {
        if (totalOrders == null || totalOrders == 0) {
            return BigDecimal.ZERO;
        }
        if (completedOrders == null) {
            return BigDecimal.ZERO;
        }
        return BigDecimal.valueOf(completedOrders)
                .divide(BigDecimal.valueOf(totalOrders), 4, BigDecimal.ROUND_HALF_UP)
                .multiply(BigDecimal.valueOf(100));
    }

    /**
     * 判断是否在线
     */
    public boolean isOnline() {
        return STATUS_ONLINE == this.status;
    }

    /**
     * 判断是否可以接单
     */
    public boolean canAcceptOrder() {
        return STATUS_ONLINE == this.status && 
               (WORK_STATUS_IDLE == this.workStatus || WORK_STATUS_WAITING == this.workStatus);
    }
}